package visiopuce.action;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;

import visiopuce.ICommandIds;
import visiopuce.ui.RechercheDossierDialog;
import visiopuce.ui.view.ResultatRechercheView;

public class RechercheDossierPopupAction extends Action {

	private final IWorkbenchWindow window;

	public RechercheDossierPopupAction(String text, IWorkbenchWindow window) {
		super(text);
		this.window = window;
		// The id is used to refer to the action in a menu or toolbar
		setId(ICommandIds.CMD_OUVRIR_RECHERCHER_DOSSIER);
		// Associate the action with a pre-defined command, to allow key
		// bindings.
		setActionDefinitionId(ICommandIds.CMD_OUVRIR_RECHERCHER_DOSSIER);
		setText("Rechercher un dossier");
		setImageDescriptor(visiopuce.Activator.getImageDescriptor("/icons/dossier_voir.png"));
	}

	public void run() {
		RechercheDossierDialog rechercheDossierDialog = new RechercheDossierDialog(window.getShell());
		if (rechercheDossierDialog.open() == IDialogConstants.OK_ID) {
			try {
				window.getActivePage().showView("visiopuce.resultatRechercheDossierView");
				ResultatRechercheView view = (ResultatRechercheView)window.getActivePage().findView("visiopuce.resultatRechercheDossierView");
				view.refresh();
			} catch (PartInitException e) {
				MessageDialog.openError(window.getShell(), "Error", "Erreur lors de l'ouverture de la vue de résultat de recherche :" + e.getMessage());
			}
		}
	}
}